rm(list=ls()) #clear

library(summarytools)
library(expss)
library(tidyverse)
library(psych)

std01<-function(x){
  min.x<-min(x, na.rm=T)
  max.x<-max(x-min.x, na.rm=T)
  return((x-min.x)/max.x)
} 
#recode vars 0 to 1

dat<- read.csv("Study 3 - Lucid/Qual_S3.csv")

dat<- subset(dat, dat$Finished==1)
dat<- subset(dat, dat$Consent.Form==1)
dat<- subset(dat, dat$atnchk=="1,2")

#party ID
dat$pid01<- ifelse(dat$pid7<4, 0, 1)
dat$pid01f<-factor(dat$pid01, labels = c("Democrat", "Republican"))
#0 = Dem and 1 = Rep

################################### Demographic Data ##################################
freq(dat$ethnicity)
freq(dat$hispanic)
freq(dat$gender)

#education
dat$edu<- ifelse(dat$education>0, dat$education, NA)
freq(dat$edu)
dat$ba<- ifelse(dat$edu>5, 1, 0)
freq(dat$ba)

#ethnicity
dat$white<- ifelse(dat$ethnicity==1, 1, 0)
dat$hisplat<- ifelse(dat$hispanic==1 | dat$hispanic==15, 0, 1)
dat$black<- ifelse(dat$ethnicity==2, 1, 0)
dat$asian<- ifelse(dat$ethnicity== 4|dat$ethnicity== 5|
                     dat$ethnicity==6|dat$ethnicity==7|
                     dat$ethnicity==8|dat$ethnicity==9|
                     dat$ethnicity==10, 1, 0) 

freq(dat$ethnicity)
freq(dat$white)
freq(dat$hisplat)
freq(dat$black)
freq(dat$asian)

#income
dat$inc<- ifelse(dat$hhi>0, dat$hhi, NA)

#gender
dat$male<- ifelse(dat$gender==1, 1, 0)
freq(dat$male)

#age
freq(dat$age)

descr(dat[c("age", "male", "white", "black", "hisplat", "ba", "inc")])

#take out pure independents
dat<- subset(dat, dat$pid7 != 4)

freq(dat$pid01)

#aggregate aps and subscale items for both 20 and 9 versions
dat$otot20<-rowMeans(with(dat, cbind(o1, o2, o3, o4, o5, o6, o7, o8)))
dat$atot20<-rowMeans(with(dat, cbind(a1, a2, a3, a4, a5, a6)))
dat$mtot20<-rowMeans(with(dat, cbind(m1, m2, m3, m4, m5, m6)))
dat$apstot20<-rowMeans(with(dat, cbind(o1, o2, o3, o4, o5, o6, o7, o8,
                                                a1, a2, a3, a4, a5, a6,
                                                m1, m2, m3, m4, m5, m6)))

dat$otot9<-rowMeans(with(dat, cbind(o1, o6, o8)))
dat$atot9<-rowMeans(with(dat, cbind(a1, a2, a4)))
dat$mtot9<-rowMeans(with(dat, cbind(m2, m4, m6)))
dat$apstot9<-rowMeans(with(dat, cbind(o1, o6, o8,
                                               a1, a2, a4, 
                                               m2, m4, m6)))

descr(dat$apstot9)
descr(dat$otot9)
descr(dat$atot9)
descr(dat$mtot9)

# party ID extremity (RELABELED)
# 0, 1, 2 coding:
dat$pidex<- ifelse(dat$pid7==1 | dat$pid7==7, 2, 
                   ifelse(dat$pid7==2 | dat$pid7==6, 1,
                          ifelse(dat$pid7==3 | dat$pid7==5, 0, NA)))
# 0, 0.5, 1 coding:
dat$rpidex<-std01(dat$pidex)
# make 0, 1, 2 coding a factor variable
dat$fpidex<-as.factor(dat$pidex)

#ideological extremism
dat$idex<- abs(dat$ideo7-4)
dat$ridex<- std01(dat$idex)
dat$fidex<- as.factor(dat$ridex)

#warmth bias (higher score means greater gap in ft)
dat$ftinparty<- ifelse(dat$pid01==0, dat$ftdem, dat$ftrep)
dat$ftoutparty<- ifelse(dat$pid01==0, dat$ftrep, dat$ftdem)
dat$wbias<- (dat$ftinparty-dat$ftoutparty)

#ANES democratic norms (higher means more supportive of democratic norms)
cols = c("anes1", "anes2", "anes3", "anes6", "anes7", "anes8")
dat[ ,cols] = 6 - dat[ ,cols]

cols = c("anes4", "anes5")
dat[ ,cols] = 8 - dat[ ,cols]

dat$ANESdnorm<-rowMeans(with(dat, cbind(anes1, anes2, anes3, anes4, 
                                                  anes5, anes6, anes7, anes8)))
psych::alpha(with(dat, cbind(anes1, anes2, anes3, anes4, 
                                  anes5, anes6, anes7, anes8))) #.62

#not overwhelmingly consistent internally.

#Westwood et. al. Partisan Violence (higher means more support pardon and longer sentence)
dat$pvcond <- coalesce(dat$pv_condition1, dat$pv_condition2,
                            dat$pv_condition3, dat$pv_condition4,
                            dat$pv_condition5, dat$pv_condition6)
var_lab(dat$pvcond)<- "Severity of Political Crime"
dat$pvchk<- ifelse(dat$mnpchk==dat$pvcond, 1, 0)
table(dat$pvchk)
dat$pvcond<- as.factor(dat$pvcond)

dat[ ,"pv1"] = 12 - dat[ ,"pv1"]

#partisan spite (higher means more partisan spite)
dat$pspitetot<-rowMeans(with(dat, cbind(pspite1, pspite2, pspite3,
                                                  pspite4, pspite5)))
psych::alpha(with(dat, cbind(pspite1, pspite2, pspite3,
                                  pspite4, pspite5))) #.87

#authoritarian rule (higher means stronger support of authoritarian rule)
dat$artot<-rowMeans(with(dat, cbind(ar1, ar2)))
psych::alpha(with(dat, cbind(ar1, ar2))) #.76

#rules of the game (higher means more support of undemocratic attitudes)
dat<- dat %>% mutate(across(c(rotg8), ~ max(.x, na.rm = T) + 1 - .x))

dat$rotgtot<-rowMeans(with(dat, cbind(rotg1, rotg2, rotg3, rotg4,
                                                rotg5, rotg6, rotg7, rotg8,
                                                rotg9, rotg10, rotg11, rotg12)))
psych::alpha(with(dat, cbind(rotg1, rotg2, rotg3, rotg4,
                                  rotg5, rotg6, rotg7, rotg8,
                                  rotg9, rotg10, rotg11, rotg12))) #.89

#item 8 is reverse coded

#need for closure (higher means more need for closure)
dat$nfctot<-rowMeans(with(dat, cbind(nfc1, nfc2, nfc3, nfc4,
                                               nfc5, nfc6, nfc7, nfc8,
                                               nfc9, nfc10, nfc11, nfc12,
                                               nfc13, nfc14, nfc15)))
psych::alpha(with(dat, cbind(nfc1, nfc2, nfc3, nfc4,
                                  nfc5, nfc6, nfc7, nfc8,
                                  nfc9, nfc10, nfc11, nfc12,
                                  nfc13, nfc14, nfc15))) #.86

#support for undemocratic candidate (higher means more support for undemocratic candidate)
dat$uncandtot<-rowMeans(with(dat, cbind(uncand1_4, uncand2_4, uncand3_4,
                                                  uncand4_4, uncand5_4, uncand6_4)))
psych::alpha(with(dat, cbind(uncand1_4, uncand2_4, uncand3_4,
                             uncand4_4, uncand5_4, uncand6_4))) #.93

#Huddy et. al. partisan social identity (higher means stronger partisan identity)
dat$idtot<-rowMeans(with(dat, cbind(id1, id2, id3, id4)))
psych::alpha(with(dat, cbind(id1, id2, id3, id4))) #.84

#political knowledge (higher means more political knowledge and interest)
#drop 3 due to Boris Johnson leaving office in middle of survey
dat$rpk1<- ifelse(dat$pk1==2, 1, 0)
dat$rpk2<- ifelse(dat$pk2==5, 1, 0)
dat$rpk3<- ifelse(dat$pk3==2, 1, 0) #Liz Truss had just taken over when fielded
dat$rpk4<- ifelse(dat$pk4==1, 1, 0)
dat$rpk5<- ifelse(dat$pk5==3, 1, 0) #3 is correct, but ambiguous
dat$rpk6<- ifelse(dat$pk6==1, 1, 0)
dat$rpk7<- ifelse(dat$pk7==7, 1, 0) 
dat$rpk8<- ifelse(dat$pk8==1, 1, 0)
dat$pktot<-rowMeans(with(dat, cbind(rpk1, rpk2, rpk4, 
                                              rpk6, rpk7, rpk8)))

#with Senate item
psych::alpha(with(dat, cbind(rpk1, rpk2, rpk4, rpk5,
                             rpk6, rpk7, rpk8))) #.64
#without Senate item
psych::alpha(with(dat, cbind(rpk1, rpk2, rpk4, 
                                  rpk6, rpk7, rpk8))) #.67

#political interest
dat$rpi<- std01(dat$pi)

#trait ratings (higher means bigger gap in trait ratings)
dat$trpol1<- (dat$IPtr_1-dat$OPtr_1)
dat$trpol2<- (dat$IPtr_2-dat$OPtr_2)
dat$trpol3<- (dat$IPtr_3-dat$OPtr_3)
dat$trpol4<- (dat$IPtr_4-dat$OPtr_4)
dat$triptot<- rowMeans(with(dat, cbind(IPtr_1, IPtr_2, IPtr_3, IPtr_4)))
dat$troptot<- rowMeans(with(dat, cbind(OPtr_1, OPtr_2, OPtr_3, OPtr_4)))
dat$trtot<- rowMeans(with(dat, cbind(trpol1, trpol2, trpol3, trpol4)))

psych::alpha(with(dat, cbind(IPtr_1, IPtr_2, IPtr_3, IPtr_4))) #.90
psych::alpha(with(dat, cbind(OPtr_1, OPtr_2, OPtr_3, OPtr_4))) #.88
psych::alpha(with(dat, cbind(trpol1, trpol2, trpol3, trpol4))) #.89

#recode all measures to 0-1
dat<- dat %>%
  mutate_at(c("otot20", "atot20", "mtot20", "apstot20",
              "otot9", "atot9", "mtot9", "apstot9",
              "wbias", "ANESdnorm", "pv1", "pv2", "pspitetot",
              "artot", "rotgtot", "nfctot", "uncandtot",
              "idtot", "pktot", "trtot", "triptot", "troptot",
              "ftinparty", "ftoutparty",
              "ideo7", "pid7", "age", "inc", "edu"),
            funs(std01(.)))

dat<- dat[c("o1", "o2", "o3", "o4", "o5", "o6", "o7", "o8",
            "a1", "a2", "a3", "a4", "a5", "a6",
            "m1", "m2", "m3", "m4", "m5", "m6",
            "otot20", "atot20", "mtot20", "apstot20", "otot9", "atot9", "mtot9", "apstot9",
            "pid7", "pid01", "pid01f", "rpidex", "fpidex", "idtot", "ideo7", "ridex", "fidex",
            "ftinparty", "ftoutparty", "wbias", "trtot", "triptot", "troptot", "rpi", "pktot", 
            "pvcond", "pvchk", "pv1", "pv2", "rotgtot", "artot", "pspitetot",
            "ANESdnorm", "nfctot", "uncandtot",
            "ba", "inc", "age", "white", "hisplat", "male")]

save(dat, file = "Study 3 - Lucid/dataS3.RData")

dat3<- dat
save(dat3, file = "Main Analyses/dataS3.RData")

